package vn.vfriends.vcms.site.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import vn.vfriends.vcms.site.model.Category;
import vn.vfriends.vcms.site.model.HotCategory;
import vn.vfriends.vcms.site.model.Item;
import vn.vfriends.vcms.site.service.CategoryService;
import vn.vfriends.vcms.site.service.ItemService;

/**
 *
 * @author tuancn
 */
@Named
@RequestScoped
public class CategoryBean implements Serializable {
    
    @Inject private CategoryService categoryService;
    
    @Inject private ItemService itemService;
    
    private Long categoryId;
    
    private int pageNum = 1;
    
    private static final int ITEM_PER_PAGE = 5;

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
    
    public Category getCurrentCategory() {
        Category currentCategory = null;
        if(categoryId != null) {
            currentCategory = this.categoryService.findCategory(categoryId);
        }
        return currentCategory;
    }
    
    public List<Category> getCategoryList() {
        Category currentCategory = this.getCurrentCategory();
        if(currentCategory != null) {
            return currentCategory.getChildren();
        } else {
            return this.categoryService.findNoParentCategories();
        }
    }
    
    public List<Category> getHotCategories(Category category) {
        List<HotCategory> hotCategories = category.getHotCategories();
        List<Category> categories = new ArrayList<Category>();
        for(HotCategory hotCategory : hotCategories) {
            categories.add(hotCategory.getCategory());
        }
        return categories;
    }
    
    public List<Item> getItemList() {
        Category currentCategory = this.getCurrentCategory();
        int firstResult = (pageNum == 0) ? 0 : (pageNum - 1) * ITEM_PER_PAGE;
        List<Item> itemList = this.itemService.findItems(currentCategory, ITEM_PER_PAGE, firstResult);
        return itemList;
    }
    
    public List<Integer> getPageNumList() {
        Category currentCategory = this.getCurrentCategory();
        List<Integer> pageNumList = new ArrayList<Integer>();
        if(currentCategory != null) {
            int itemCount = this.itemService.getPublishedItemCount(currentCategory);
            int pageCount = 0;
            if(itemCount%ITEM_PER_PAGE > 0) {
                pageCount = Double.valueOf(Math.floor(itemCount / ITEM_PER_PAGE)).intValue() + 1;
            } else {
                pageCount = itemCount / ITEM_PER_PAGE;
            }
            for(int i = 1; i <= pageCount; i++) {
                pageNumList.add(i);
            }
        }
        return pageNumList;
    }
}
